
function dbCtrl($scope,$rootScope,$modal) {
    //查找实例集合
    $scope.queryDbList = function () {
        dbQuery($scope);
    }

    $scope.queryDbRemarkList = function () {
        dbRemarkQuery($scope,$rootScope);
    }

    $scope.querySchemaList = function () {
        schemaQuery($scope);
    }
    $scope.querySchemaRemarkList = function () {
        schemaRemarkQuery($scope);
    }

    $scope.openBox_update = function (db) {
        $scope.db_update = db;
        var $modalInstance = $modal.open({
            templateUrl: 'views/admin_db_update_page.html',
            controller: DB_ModalCtrl,
            scope: $scope
        });
    };

    $scope.openBox_insert = function () {
        var $modalInstance = $modal.open({
            templateUrl: 'views/admin_db_insert_page.html',
            controller: DB_ModalCtrl,
            scope: $scope
        });
    };

    $scope.openBox_dbRemark_update = function (dbRemark) {
        $scope.dbRemark_update = dbRemark;
        console.log($scope.dbRemark_update);
        var $modalInstance = $modal.open({
            templateUrl: 'views/admin_db_remark_update_page.html',
            controller: DB_ModalCtrl,
            scope: $scope
        });
    };

    $scope.openBox_dbRemark_insert = function(){
        var $modalInstance = $modal.open({
            templateUrl: 'views/admin_db_remark_insert_page.html',
            controller: DB_ModalCtrl,
            scope: $scope
        });
    }

    $scope.deleteDb = function(){
        var ck_db_Id = $("input:checkbox[name=ck_db_Id]:checked");
        if(ck_db_Id.length==0){
            alert('至少选择一个数据库实例');
            return;
        }
        var arr = [];
        for(var i=0;i<ck_db_Id.length;i++){
            arr.push(ck_db_Id[i].value);
        }
        if (confirm("确认要删除？")) {
            dbDelete($scope,arr);
        }
    }

    $scope.updateDbSchema = function(){
        var ck_db_Id = $("input:checkbox[name=ck_db_Id]:checked");
        if(ck_db_Id.length==0){
            alert('至少选择一个数据库实例');
            return;
        }
        var arr = [];
        for(var i=0;i<ck_db_Id.length;i++){
            arr.push(ck_db_Id[i].value);
        }
        if (confirm("确认更新吗？")) {
            updateDbSchema($scope,arr);
        }
    }
}

function updateDbSchema($scope,arr){
    var paramObj = new Object();
    paramObj.dbIdStr = arr.join(',');
    $.ajax({
        url: '/db/doAutoReadSchemas',
        data: paramObj,
        type:'post',
        success: function (data) {
            var responseObj = $.parseJSON(data);
            if (responseObj.success) {
                alert('更新成功');
            } else {
                alert(responseObj.errorMsg);
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}
function dbDelete($scope,dbIdArr){
    var paramObj = new Object();
    paramObj.dbIdStr = dbIdArr.join(',');
    $.ajax({
        url: '/db/doDeleteDb',
        data: paramObj,
        type:'post',
        success: function (data) {
            var responseObj = $.parseJSON(data);
            if (responseObj.success) {
                var arr = new Array();
                for(var i=0;i<dbIdArr.length;i++){
                    for(var j=0;j<$scope.dataList_db.length;j++){
                        if($scope.dataList_db[j].id==dbIdArr[i]){
                            arr.push(j);
                        }
                    }
                }
                alert('删除成功');
                $scope.$apply(function () {
                    for(var j=0;j<arr.length;j++){

                        for(var i=0,n=0;i<$scope.dataList_db.length;i++)
                        {
                            if($scope.dataList_db[i]!=$scope.dataList_db[arr[j]])
                            {
                                $scope.dataList_db[n++]=$scope.dataList_db[i]
                            }
                        }
                        $scope.dataList_db.length-=1
                    }
                });
            } else {
                alert(responseObj.errorMsg);
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}
function DB_ModalCtrl($scope, $modalInstance) {

    //更新操作
    $scope.update = function () {
        if (confirm("确认要更新吗？")) {
            dbUpdate($scope);
            $modalInstance.close();
        }
    };

    //更新操作
    $scope.dbRemarkUpdate = function () {
        if (confirm("确认要更新吗？")) {
            dbRemarkUpdate($scope);
        }
        $modalInstance.close();
    };


    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };

    $scope.save = function(){
        var db = $scope.db_insert;
        if(db.passWord!=db._passWord){
            alert('两次密码输入不一致,请重新输入');
            return;
        }
        if (confirm("确认要插入吗？")) {
            dbInsert($scope);
            $modalInstance.close();
        }

    }

    $scope.insertDbRemark = function(){
        insertDbRemark($scope);
        $modalInstance.close();
    }

    $scope.deleteDbRemark = function(){
        $modalInstance.close();
    }

    $scope.testConnection = function () {
        testConnection($scope);
    }
};

function deleteDbRemark($scope){

}
function insertDbRemark($scope){
    $.ajax({
        type:'post',
        url: '/db/doInsertDbRemark',
        data: $scope.dbRemark_insert,
        success: function (data) {
            var responseObj = $.parseJSON(data);
            if (responseObj.success) {
               alert('插入成功');
            } else {
                alert(responseObj.errorMsg);
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}

//数据库实例查询操作
function dbQuery($scope, pageNo) {
    if (!pageNo || pageNo == 0) {
        pageNo = 1;
    }

    var paramObj = new Object();
    paramObj.pageNo = pageNo;

    $.ajax({
        url: '/db/doReadDbList',
        data: paramObj,
        success: function (data) {
            var responseObj = $.parseJSON(data);
            if (responseObj.success) {
                $scope.$apply(function () {
                    $scope.dataList_db = responseObj.dataList;
                });
            } else {
                alert(responseObj.errorMsg);
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}

//数据库实例更新操作
function dbUpdate($scope){
    var db = $scope.db_update;
    $.ajax({
        type:'post',
        url: '/db/doUpdateDb',
        data:{
            'dbName':db.dbName,
            'id':db.id,
            'loginName':db.loginName,
            'passWord':db.passWord,
            'statusFlag':db.statusFlag,
            'url':db.url
        },
        success: function (data) {
            var responseObj = $.parseJSON(data);
            if (responseObj.success) {
                alert('更新成功');
            } else {
                alert(responseObj.errorMsg);
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}

function dbRemarkUpdate($scope){
    var dbRemark = $scope.dbRemark_update;
    $.ajax({
        type:'POST',
        url: '/db/doUpdateDbRemark',
        data: {
            'id':dbRemark.id,
            'dbName':dbRemark.dbName,
            'dbId':dbRemark.dbId,
            'dbRemark':dbRemark.dbRemark,
            'userId':dbRemark.userId,
            'statusFlag':dbRemark.statusFlag
        },
        success: function (data) {
            var responseObj = $.parseJSON(data);
            if (responseObj.success) {
                alert('更新成功');
            } else {
                alert(responseObj.errorMsg);
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}
//数据库实例插入操作
function dbInsert($scope){
    $.ajax({
        url: '/db/doInsertDb',
        data: $scope.db_insert,
        type:'post',
        success: function (data) {
            var responseObj = $.parseJSON(data);
            if (responseObj.success) {
                $scope.dataList_db.push(responseObj.db);
                alert('插入成功');
            } else {
                alert(responseObj.errorMsg);
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}

function dbRemarkQuery($scope,$rootScope, pageNo) {
    if (!pageNo || pageNo == 0) {
        pageNo = 1;
    }

    var paramObj = new Object();
    paramObj.pageNo = pageNo;
    paramObj.userId = $rootScope.systemUser.id;

    $.ajax({
        url: '/db/doReadDbRemarkListByUserId',
        data: paramObj,
        success: function (data) {
            var responseObj = $.parseJSON(data);
            if (responseObj.success) {
                $scope.$apply(function () {
                    $scope.dataList_dbremark = responseObj.dbRemarkList;
                    $scope.dataList_schema = responseObj.dataList;
                    console.log($scope.dataList_dbremark);
                    console.log($scope.dataList_schema)
                });
            } else {
                alert(responseObj.errorMsg);
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}
function schemaQuery($scope, pageNo) {
    if (!pageNo || pageNo == 0) {
        pageNo = 1;
    }

    var paramObj = new Object();
    paramObj.pageNo = pageNo;

    $.ajax({
        url: '/db/doReadSchemaList',
        data: paramObj,
        success: function (data) {
            var responseObj = $.parseJSON(data);
            if (responseObj.success) {
                $scope.$apply(function () {
                    $scope.dataList = responseObj.dataList;
                });
            } else {
                alert(responseObj.errorMsg);
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}
function schemaRemarkQuery($scope, pageNo) {
    if (!pageNo || pageNo == 0) {
        pageNo = 1;
    }

    var paramObj = new Object();
    paramObj.pageNo = pageNo;

    $.ajax({
        url: '/db/doReadSchemaRemarkList',
        data: paramObj,
        success: function (data) {
            var responseObj = $.parseJSON(data);
            if (responseObj.success) {
                $scope.$apply(function () {
                    $scope.dataList = responseObj.dataList;
                });
            } else {
                alert(responseObj.errorMsg);
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}

//测试数据库链接是否可用
function testConnection($scope){
    $.ajax({
        type:'post',
        url: '/db/doTestConnection',
        data: $scope.db_insert,
        success: function (data) {
            var responseObj = $.parseJSON(data);
            if (responseObj.success) {
                $scope.$apply(function () {
                    if(responseObj.userful){
                        alert('连接可用!');
                    }else{
                        alert('连接不可用');
                    }
                });
            } else {
                alert(responseObj.errorMsg);
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}
angular
    .module('inspinia')
    .controller('dbCtrl', dbCtrl)